<template>
  <view class="register-container">
    <text class="title">红木家具平台注册</text>
    
    <uni-forms ref="form" :model="formData">
      <!-- 用户名输入 -->
      <uni-forms-item name="username" required>
        <uni-easyinput 
          v-model="formData.username" 
          placeholder="请设置用户名(3-20位)"
          prefixIcon="person"
        />
      </uni-forms-item>
      
      <!-- 密码输入 -->
      <uni-forms-item name="password" required>
        <uni-easyinput 
          v-model="formData.password" 
          placeholder="请设置密码(6-20位)" 
          type="password"
          prefixIcon="locked"
        />
      </uni-forms-item>
      
      <!-- 确认密码 -->
      <uni-forms-item name="confirmPassword" required>
        <uni-easyinput 
          v-model="formData.confirmPassword" 
          placeholder="请确认密码" 
          type="password"
          prefixIcon="locked"
        />
      </uni-forms-item>
      
      <!-- 注册按钮 -->
      <button 
        class="register-btn"
        :loading="registering"
        @click="handleRegister"
        :disabled="registering"
      >
        {{ registering ? '注册中...' : '注册账号' }}
      </button>
      
      <!-- 去登录链接 -->
      <view class="login-link">
        <text>已有账号？</text>
        <text class="link" @click="navToLogin">去登录</text>
      </view>
    </uni-forms>
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      registering: false
    }
  },
  methods: {
    async handleRegister() {
      // 表单验证
      if (!this.validateForm()) return;
      
      this.registering = true;
      
      try {
        // 调用云函数注册
        const result = await uniCloud.callFunction({
          name: 'user',
          data: {
            action: 'register',
            params: {
              username: this.formData.username,
              password: this.formData.password
            }
          }
        });
        
        const response = result.result;
        
        if (response.code === 0) {
          uni.showToast({
            title: '注册成功！',
            icon: 'success',
            duration: 1500
          });
          
          // 自动跳转到登录页面
          setTimeout(() => {
            this.navToLogin();
          }, 1600);
        } else {
          this.showError(response.message || '注册失败');
        }
      } catch (e) {
        this.showError('网络请求失败');
        console.error('调用失败:', e);
      } finally {
        this.registering = false;
      }
    },
    
    validateForm() {
      if (!this.formData.username.trim() || this.formData.username.length < 3) {
        this.showError('用户名需至少3位');
        return false;
      }
      
      if (!this.formData.password || this.formData.password.length < 6) {
        this.showError('密码需至少6位');
        return false;
      }
      
      if (this.formData.password !== this.formData.confirmPassword) {
        this.showError('两次输入的密码不一致');
        return false;
      }
      
      return true;
    },
    
    showError(msg) {
      uni.showToast({
        title: msg,
        icon: 'none',
        duration: 2000
      });
    },
    
    navToLogin() {
      uni.navigateTo({
        url: '/pages/users/login'
      });
    }
  }
}
</script>

<style scoped>
/* 样式保持不变 */
</style>